package com.ley.yikang.service.serviceimpl;

import com.ley.yikang.entity.Community;
import com.ley.yikang.mapper.CommunityMapper;
import com.ley.yikang.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommunityServiceImpl implements CommunityService {
    private final CommunityMapper communityMapper;

    @Autowired
    public CommunityServiceImpl(CommunityMapper communityMapper) {
        this.communityMapper = communityMapper;
    }

    @Override
    public void addCommunity(Community community) {
        communityMapper.addCommunity(community);
    }

    @Override
    public List<Community> findAll(String name, String address, String phone) {
        // 使用Map封装可选参数
        Map<String, Object> params = new HashMap<>();
        if (name != null && !name.isEmpty()) {
            params.put("name", name);
        }
        if (address != null && !address.isEmpty()) {
            params.put("address", address);
        }
        if (phone != null && !phone.isEmpty()) {
            params.put("phone", phone);
        }
        return communityMapper.findAll(params);
    }

    @Override
    public Community findCommunityById(int id) {
        return communityMapper.findCommunityById(id);
    }

    @Override
    public Community findCommunityByName(String name) {
        return communityMapper.findCommunityByName(name);
    }

    @Override
    public void delCommunity(int id) {
        communityMapper.delCommunity(id);
    }

    @Override
    public void updateCommunity(Community community) {
        communityMapper.updateCommunity(community);
    }
}
